/**
 * 
 */
package com.changil.nowonchangilchurch.ui.item;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.changil.nowonchangilchurch.R;

/**
 * @author zeropol2
 *
 */
public class Header extends LinearLayout {
	private Context mContext = null;
	private LinearLayout mLayout = null;
	private LinearLayout mPrevButtonLayout = null;
	private TextView mHeaderText = null;
	private ImageView mShareBtn = null;
	public interface UserActionListener{
		public void onShareBtnClick();
	}
	private UserActionListener mUserActionListener = null;
	/**
	 * @param context
	 */
	public Header(Context context) {
		super(context);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public Header(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public Header(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context) {
		mContext = context;
		this.setBackgroundColor(0xFF2C3640);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = (LinearLayout) inflater.inflate(R.layout.header, this,true);
		mHeaderText = (TextView)mLayout.findViewById(R.id.header_text);
		mPrevButtonLayout = (LinearLayout)mLayout.findViewById(R.id.header_prev_button_layout);
		mPrevButtonLayout.setOnClickListener(mOnClickListener);
		mShareBtn = (ImageView)mLayout.findViewById(R.id.header_action_share);
		mShareBtn.setOnClickListener(mOnClickListener);
	}
	
	public void setHeaderText(String text) {
		mHeaderText.setText(text);
	}
	
	public void setHeaderColor(int resId) {
		this.setBackgroundResource(resId);
	}
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			if(mPrevButtonLayout.getId() == v.getId()) {
				((Activity)mContext).finish();
			} else if(mShareBtn.getId() == v.getId()) {
				if(null != mUserActionListener) {
					mUserActionListener.onShareBtnClick();
				}
			}
			
		}
	};

	public void setUserActionListener(UserActionListener l) {
		mUserActionListener = l;
	}
	
	public void setShareBtnEnabled(boolean isEnabled) {
		if(isEnabled) {
			mShareBtn.setVisibility(View.VISIBLE);
		} else {
			mShareBtn.setVisibility(View.GONE);
		}
	}
}
